package com.codingman.mybatisdemo4;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionManager;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.SqlBuilder;

public class Demo1 {

	public static SqlSessionManager getSqlSessionManager() {
		InputStream is;
		try {
			is = Resources.getResourceAsStream("mybatis_config.xml");
			return SqlSessionManager.newInstance(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		SqlSessionManager manager = Demo1.getSqlSessionManager();
		
		EmployeeMapper dao = manager.getMapper(EmployeeMapper.class);
		
		EmployeeTable table = new EmployeeTable();
		SelectStatementProvider selectStatement = SqlBuilder.select(table.id, table.name, table.salary)
	        .from(table)
	        .where(table.id, SqlBuilder.isEqualTo(101))
	        .build()
	        .render(RenderingStrategies.MYBATIS3);
		
		Employee e1 = dao.getEmployeeById(selectStatement).get();
		
		System.out.println(e1);
	}
}
